/**
 * 批量增加视频列表初始化
 */
var videoTableWidth = 800; //表格宽度
var videoTableHeight = 500; //表格高度
var videoTablePageSize = 15; //表格默认记录条数
var videoTablePageList = [5,10,15,20,25,30,35,40]; //表格页面记录列表
var videoPageNumber =1;
var videoTableTitle = "视频信息表";
var videoTableURL = getTimeURL("../video/videoInfo/selectPageForBackCoursePackage.do?disableFlag=0");
var videoTableQueryParams; //表格请求参数

var addVideoesUrl ="../coursepackage/coursePackageVideoRel/insertVideos.do";

var videoTableColumns = [ [
		{
			field : 'ck',
			checkbox : true
		},
		{
			field : 'id',
			title : 'id',
			width : 0,
			rowspan : 1
		},
		{
			field : 'videoName',
			title : '视频名称',
			width : 200,
			align : 'center',
			rowspan : 1
		},
		 {
			field : 'videoDesc',
			title : '视频描述',
			width : 200,
			align : 'center',
			rowspan : 1
		}, {
			field : 'userName',
			title : '主讲人',
			width : 100,
			align : 'center',
			rowspan : 1
		}, {
			field : 'typeName',
			title : '视频类型',
			width : 200,
			rowspan : 1
		}
		] ];

/**
 * 装载视频表格第1页数据
 */
function loadVideoPageData(coursePackageId) {
	var videoToolbarVar;
	var addVideoListBar = {
			id : 'btncut',
			text : '添加视频',
			iconCls : 'icon-add',
			handler : function() {
				addVideoes("批量增加",coursePackageId); 
			}
		};	
	
	videoToolbarVar = [addVideoListBar];		
	
	// datagrid表格
	$('#datagrid_table_video').datagrid({
		title : videoTableTitle,
		iconCls : 'icon-save',
		/*width : tableWidth,*/
		height : videoTableHeight,
		nowrap : false,
		striped : true,	
		url : videoTableURL,
		pageNumber : videoPageNumber, // 指定当前页码为第1页
		remoteSort : true,
		idField : 'id',
		pagination : true, // 支持分页		
		pageSize: videoTablePageSize,
		pageList: videoTablePageList,
		pagePosition:'top',
		queryParams: videoTableQueryParams,
		rownumbers : true, // 显示记录编号
		toolbar : videoToolbarVar,		
		checkOnSelect:true,
		onLoadSuccess : function(data) {
			setTimeout(function() {	
				$("#datagrid_table_video").datagrid("clearSelections");
				$("#datagrid_table_video").datagrid("clearChecked");
				$(".xxl_linkbutton").linkbutton({plain:true});
			}, 10);
		},
		columns : videoTableColumns
	});
	$('#datagrid_table_video').datagrid('hideColumn','id'); //隐藏id字段	
}

//添加视频
function addVideoRecords(){	
	var rows = $('#datagrid_table').datagrid('getSelections');	
	if(isSelected(rows,'批量增加视频')){
		if(rows.length != 1){
			$.messager.alert('批量添加视频','只能选择一条记录进行批量添加视频');
			return false;
		}
		var coursePackageId = rows[0].id;
		$('#datagrid_table').datagrid('clearSelections'); //清除所有已选择的记录，避免重复提交id值
		$('#datagrid_add_video').css("display","block");
		// 打开添加窗口
		$('#datagrid_add_video').window({
			title : "批量添加视频",
			modal : true,
			width : 830,
			height : 520,
			shadow : false,
			closed : false,
			minimizable : false,
			maximizable : false,
			collapsible : false,
			iconCls : 'icon-add',
			resizable : false,
			//top:($(window).height() - addHeight) * 0.5,
			top : 50,
			left : ($(window).width() - 830) * 0.5,
			onOpen : function(){
				//loadKindeditor();
				//KindEditor.ready(function(K){		
				if(typeof KindEditor != "undefined"){
				editor=KindEditor.create('textarea[class="kindeditor"]', {
					resizeType : 1,
					allowPreviewEmoticons : false,
					uploadJson : '/mrjy_file/common/util/uploadImg.do',
					allowImageUpload : true,
					afterBlur: function(){
						this.sync();
					},
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link']
				});
				}
			    //});
			},
			onClose: function () {
				// 关闭Dialog前移除编辑器
				  if(typeof KindEditor != "undefined"){
				   KindEditor.remove('.kindeditor');
				  }
			}
		});
		initEvent();
		loadVideoPageData(coursePackageId);//加载表格数据		
	}	 
}

 function videoReload() {
	$('#datagrid_table_video').datagrid('load', {});
};

function videoClearForm() {
	
}
/**
 * 条件查询操作
 */
function videoSearchTable() {
	var form = $('#videoSearchForm');
	$("#datagrid_table_video").datagrid("load", serializeForm(form));
}

function initEvent(){
	$("#videoUserIdQuery").combobox({
		url : "../maintain/userInfo/userListNoPage.do?roleId=2",
		valueField : 'id',
		textField : 'text',
		editable : true,
		onSelect : function(data) {
		}
	});
}
//批量增加视频
function addVideoes(title,coursePackageId){
	var title = "批量增加"
	var rows = $('#datagrid_table_video').datagrid('getSelections');	
	if(isSelected(rows,title)){
		$.messager.confirm(title + '记录', '您确定要' + title + '已选中的记录?', function(r){ 	
			if(r){					
				var videoIds = "?coursePackageId=" + coursePackageId + "&";
				for(var i=0;i<rows.length-1;i++){									
					videoIds += "videoIds=" + rows[i].id + "&";					
				}
				videoIds += "videoIds=" + rows[i].id;
				$.get(addVideoesUrl + videoIds,function(data){
					if(data.err_code == 0){
						$.messager.alert(title + '成功','成功' + title + rows.length + "条记录！",'info');
						$('#datagrid_add_video').window('close');
					}else if(data.err_code == 1){
						$.messager.alert(title + '成功',"所选的记录已经添加过了",'info');
					}else{
						$.messager.alert(title + '失败',title + '失败','error');
					}
										
				},"json");
			}	
		});	
	}	
}